<template>
  <el-dialog @closed="resetFrom" :title="!form.id ? '新增' : '修改'" :visible.sync="visible">
    <el-form ref="dataForm" :model="form" :rules="rules" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="上级指标">
            <treeselect
              v-model="form.pid"
              :options="indexOptions"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级指标" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="名称" prop="indexName">
        <el-input v-model="form.indexName" placeholder="请输入指标名称" />
      </el-form-item>
      <el-form-item label="指标Code">
        <el-input v-model="form.indexCode" placeholder="请输入指标Code" />
      </el-form-item>
      <el-form-item label="指标介绍">
        <el-input type="textarea" :rows="3" v-model="form.indexDescription" placeholder="请输入指标介绍" />
      </el-form-item>
      <el-form-item label="支撑材料说明">
        <el-input type="textarea" :rows="3" v-model="form.supportMaterial" placeholder="请输入支撑材料说明" />
      </el-form-item>
      <el-row>
        <el-col :span="12">
          <el-form-item label="排序" prop="sortOrder">
            <el-input-number v-model="form.sortOrder" controls-position="right" :min="0" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit">确 定</el-button>
      <el-button @click="visible = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getTree, getObj, addObj, putObj } from "@/api/sysindex"
import Treeselect from "@riophae/vue-treeselect"
import TableForm from "./"
import "@riophae/vue-treeselect/dist/vue-treeselect.css"

export default {
  name: "Menu",
  components: { Treeselect, TableForm },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 指标树选项
      indexOptions: [],
      // 是否显示弹出层
      visible: false,
      form: {
        indexName: undefined,
        indexCode: undefined,
        indexDescription: undefined,
        supportMaterial: undefined,
        pid: undefined,
        sortOrder: 999,
      },
      // 表单校验
      rules: {
        indexName: [{ required: true, message: "指标名称不能为空", trigger: "blur" }],
        sortOrder: [{ required: true, message: "指标顺序不能为空", trigger: "blur" }],
      },
    }
  },
  methods: {
    init(isEdit, id) {
      if (id != null) {
        this.form.pid = id
      }
      this.visible = true
      this.getTreeselect()
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields()
        if (isEdit) {
          getObj(id).then((response) => {
            this.form = response.data.data
          })
        } else {
          this.form.id = undefined
        }
      })
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          if (this.form.pid === undefined) {
            this.form.pid = -1
          }

          if (this.form.id) {
            putObj(this.form).then((data) => {
              this.$message.success("修改成功")
              this.visible = false
              this.$emit("refreshDataList")
            })
          } else {
            addObj(this.form).then((data) => {
              this.$message.success("添加成功")
              this.visible = false
              this.$emit("refreshDataList")
            })
          }
        }
      })
    },
    /** 查询指标下拉树结构 */
    getTreeselect() {
      getTree().then((res) => {
        this.indexOptions = []
        const tree = { id: -1, name: "根指标", children: [] }
        tree.children = res.data.data
        this.indexOptions.push(tree)
      })
    },
    /** 转换指标数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.id,
        label: node.name,
        children: node.children,
      }
    },
    resetFrom() {
      Object.keys(this.form).forEach((key) => {
        this.form[key] = undefined
      })
      this.form.sortOrder = 999
    },
  },
}
</script>
